// RotatePolygon.java
// by Alexandre Martins, 2013
// MAC 5857 - Prof. Leonidas

package example.ilm.model;

import ilm.framework.assignment.model.DomainAction;
import ilm.framework.domain.DomainModel;

public class RotatePolygon extends DomainAction {

    private IlmDomainModel _domain;
    private Polygon polygon;
    private double angle;
    private static int cnt = 0;

    public RotatePolygon() {
        super("rotatePolygon[" + (++cnt) + "]", "rotated a polygon");
        this.angle = 0.0;
    }

    public void setPolygon(Polygon polygon) {
        this.polygon = polygon;
    }

    public void rotate(double angle) {
        this.angle = angle;
    }

    /**
     * Defines the DomainModel on which this action can happen. It must be
     * converted to IlmDomainModel so this class can call its specific methods.
     * 
     * @param an
     *            IlmDomainModel
     */
    public void setDomainModel(DomainModel model) {
        _domain = (IlmDomainModel) model;
    }

    /**
     * Method that is called when this action is executed.
     */
    protected void executeAction() {
        _domain.rotatePolygon(_currentState, polygon, angle);
    }

    /**
     * Method that is called when this action is undone.
     */
    protected void undoAction() {
        _domain.rotatePolygon(_currentState, polygon, -angle);
    }

    /**
     * Compares this to another DomainAction.
     */
    public boolean equals(DomainAction a) {
        RotatePolygon b = (RotatePolygon)a;
        return (getName().equals(a.getName()) && getDescription().equals(a.getDescription()) && polygon.equals(b.polygon) && Math.abs(angle - b.angle) < 1e-5);
    }

    /**
     * This method is used for converting this object into string
     * 
     * @see example.ilm.model.IlmDomainConverter
     */
    public String toString() {
        return "" + 
            "<rotatepolygon>" + 
                "<name>" + getName() + "</name>" + 
                "<description>" + getDescription() + "</description>" + 
                "<angle>" + angle + "</angle>" + 
                "<polygon>" + 
                    polygon.toString() + 
                "</polygon>" + 
            "</rotatepolygon>";
    }
}
